﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Timers;

namespace PersonaLibrary.Validation
{
    public class ValidationItem : INotifyPropertyChanged
    {
    	private ValidationPair _validationValue;
        private bool _isValid;
        private bool _moreThenOneCand;
    	private RelayCommand _validateCommand;

    	public ValidationItem()
    	{
			AvailableItems = new ObservableCollection<ValidationPair>();
			ValidationValue = new ValidationPair();
    	}

        public bool IsValid
        {
            set
            {
                _isValid = value;
                RisePropertyChanged("IsValid");
            }
            get
            {
                return _isValid;
            }
        }

        public ValidationPair ValidationValue
        {
            set
            {
                _validationValue = value;
                RisePropertyChanged("ValidationValue");
            }
            get
            {
                return _validationValue;
            }
        }

    	public RelayCommand ValidateCommand
    	{
    		get
    		{
				if(_validateCommand == null)
					_validateCommand = new RelayCommand(x => Validate(), x => true);
    			return _validateCommand;
    		}
    	}

    	public List<ValidationPair> KeyValueCollection { set; get; }

		public ObservableCollection<ValidationPair> AvailableItems { private set; get; }

    	public bool MoreThenOneCand
        {
            set
            {
                _moreThenOneCand = value;
                RisePropertyChanged("MoreThenOneCand");
            }
            get
            {
                return _moreThenOneCand;
            }
        }
		
        public event PropertyChangedEventHandler PropertyChanged;

		private void Validate()
		{
			string validateValue = ValidationValue.Key;
			AvailableItems.Clear();
			foreach (ValidationPair keyValuePair in KeyValueCollection.Where(x => x.Key.StartsWith(validateValue, true, CultureInfo.InvariantCulture)))
			{
				AvailableItems.Add(keyValuePair);
			}

			if (AvailableItems.Count == 1)
			{
				IsValid = true;
				ValidationValue = AvailableItems[0];
				MoreThenOneCand = false;
			}
			else if(AvailableItems.Count == 0)
			{
				IsValid = false;
				ValidationValue = new ValidationPair();
				MoreThenOneCand = false;
			}
			else if(AvailableItems.Count > 1)
			{
				MoreThenOneCand = true;
			}
			else
				throw new Exception("Произошла ошибка обратитесь к разработчику");
		}

        protected void RisePropertyChanged(string name)
        {
            if (PropertyChanged != null) 
				PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
		
		public class ValidationPair : INotifyPropertyChanged
		{
			private string _key;
			private int _value;

			public string Key
			{
				get { return _key; }
				set
				{
					_key = value;
					RisePropertyChanged("Key");
				}
			}

			public int Value
			{
				get { return _value; }
				set
				{
					_value = value;
					RisePropertyChanged("Value");
				}
			}

			private void RisePropertyChanged(string name)
			{
				if (PropertyChanged != null)
					PropertyChanged(this, new PropertyChangedEventArgs(name));
			}

			public event PropertyChangedEventHandler PropertyChanged;

			public override string ToString()
			{
				return _key;
			}
		}
    }
}
